﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleKit.other
{
    public class xmlparser
    {
        [XmlRoot()]
        public class Books
        {
            private Book[] books;

            /// <summary>
            ///
            /// </summary>
            public Book[] BookInput
            {
                get { return books; }
                set { books = value; }
            }
        }

        public class Book
        {
            private string name;
            private string author;

            [XmlAttribute]
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            [XmlAttribute]
            public string Author
            {
                get { return author; }
                set { author = value; }
            }
        }  

        public static string ParsingXML(string input)
        {
            string output = string.Empty;

            XmlSerializer serializer = new XmlSerializer(typeof(Books));
            Books ei = (Books)serializer.Deserialize(new StringReader(input));
            output += "Books\r\n";
            int counter=1;
            foreach (Book b in ei.BookInput)
            {
                output += "\tBook " + counter + "\r\n";
                output += "\t\tName:" + b.Name + "\r\n";
                output += "\t\tAuthor:" + b.Author + "\r\n";
            }

            return output;
        }

        public static void xmlparserMain(string[] args)
        {
            //xml is not standard xml
            string input = "<?xml version=\"1.0\" ?><Books><Book><Name = \"The C++ Programming Language\" Author=\"Bjarne Stroustrup\" /></Book><Book><Name = \"Effective C++\" Author = \"Scott Meyers\" /></Book></Books>";

            //Console.Write(ParsingXML(input));

        }
    }
}
